//
//  MainTabBarController.m
//  CMVRFForIOS
//
//  Created by HailongHan on 15/3/27.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "MainTabBarController.h"
#import "Project_TabViewController.h"
#import "Product_TabViewController.h"
#import "Resource_TabViewController.h"
#import "Mine_TabViewController.h"
#import <RDVTabBarItem.h>
#import "KKNavigationController.h"
#import "UINavigationController+YRBackGesture.h"
#import "CommonResponse.h"
#import "BigMachine_TabViewController.h"

@interface MainTabBarController ()<RDVTabBarControllerDelegate> {
    int unReadCountForNotice;
    int unReadCountForSuggestion;
}

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    unReadCountForNotice = 0;
    unReadCountForSuggestion = 0;
    // Do any additional setup after loading the view.
    [self setupViewControllers];
//    [self loadUnreadNoticeCount];
//    [self loadUnreadSuggestionCount];
    
    kProject = [[ProjectManager manager] newProject];
    CMLog(@"%@", kProject.requirement);
    
    //注册通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"refreshNoticeCount" object:nil]; //先取消注册
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadUnreadNoticeCount) name:@"refreshNoticeCount" object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"refreshSuggestionCount" object:nil]; //先取消注册
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadUnreadSuggestionCount) name:@"refreshSuggestionCount" object:nil];
}

#pragma mark - RDV高度自定的tabbar
- (void)setupViewControllers{
    
    UINavigationController *project_nav = [[KKNavigationController alloc] initWithRootViewController:[Project_TabViewController new]];
    
    UINavigationController *machine_nav = [[KKNavigationController alloc] initWithRootViewController:[BigMachine_TabViewController new]];
    
    UINavigationController *product_nav = [[KKNavigationController alloc] initWithRootViewController:[Product_TabViewController new]];
    
//    UINavigationController *resource_nav = [[KKNavigationController alloc] initWithRootViewController:[Resource_TabViewController new]];
    
    UINavigationController *mine_nav = [[KKNavigationController alloc] initWithRootViewController:[Mine_TabViewController new]];
    
    [self setViewControllers:@[project_nav,machine_nav,product_nav,mine_nav]];
    
    //设置TabbarItem
    [self customizeTabBarForController];
    self.delegate = self;

}

- (void)customizeTabBarForController{
    UIImage *backgroundImage = [UIImage imageNamed:@"tabbar_background"];
    NSArray *tabBarItemImages = @[@"tab_project", @"tab_resource", @"tab_product", @"tab_mine"];
    NSArray *tabBarItemTitles = @[@"5C", @"项目", @"产品", @"设置"];
    
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[self tabBar] items]) {
        [item setBackgroundSelectedImage:backgroundImage withUnselectedImage:backgroundImage];
        if (iPhone_X) {
            item.itemHeight = kTab_Bar_Height + KIphoneX_Adjust_Height;
            item.imagePositionAdjustment = UIOffsetMake(0, -5);
        } else {
            item.itemHeight = kTab_Bar_Height;
        }
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",
                                                      [tabBarItemImages objectAtIndex:index]]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",
                                                        [tabBarItemImages objectAtIndex:index]]];
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
        [item setTitle:[tabBarItemTitles objectAtIndex:index]];
        
        //设置字体
        // 设置tabBarItem的普通文字颜色
        NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
        textAttrs[NSForegroundColorAttributeName] = CM_DARKGRAY_COLOR;
        [item setUnselectedTitleAttributes:textAttrs];
        
        // 设置tabBarItem的选中文字颜色
        NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
        selectedTextAttrs[NSForegroundColorAttributeName] = CM_ORANGE_COLOR;
        [item setSelectedTitleAttributes:selectedTextAttrs];
        
        
        //设置BadgeView
        if (index == 2 && unReadCountForNotice > 0) {
            //公告
            [item setBadgeBackgroundColor:[UIColor redColor]];
            [item setBadgeTextColor:[UIColor whiteColor]];
            [item setBadgeValue:[NSString stringWithFormat:@"%d", unReadCountForNotice]];
            
            //在桌面图标上显示消息数
            [UIApplication sharedApplication].applicationIconBadgeNumber = unReadCountForNotice;
        }
        else if (index == 3 && unReadCountForSuggestion > 0) {
            //意见
            [item setBadgeBackgroundColor:[UIColor redColor]];
            [item setBadgeTextColor:[UIColor whiteColor]];
            [item setBadgeValue:[NSString stringWithFormat:@"%d", unReadCountForSuggestion]];
        }
        
        index++;
    }
}

- (BOOL)tabBarController:(RDVTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    return YES;
}

- (void)tabBarController:(RDVTabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
}

#pragma mark -加载数据

- (void) loadUnreadNoticeCount {
    [HttpClient getWithUrl:HTTP_GET_UNREAD_NOTICES token:[[AccountManager manager]token] paramDic:nil resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
        unReadCountForNotice = obj.getUnReadPostResult;
        [self setNoticeBarView];
    } failure:^(NSError *error) {
        
    }];
}

- (void) setNoticeBarView {
    
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[self tabBar] items]) {
        //设置BadgeView
        if (index == 2) {
            [item setBadgeBackgroundColor:[UIColor redColor]];
            [item setBadgeTextColor:[UIColor whiteColor]];
            if (unReadCountForNotice > 0) {
                [item setBadgeValue:[NSString stringWithFormat:@"%d", unReadCountForNotice]];
            }
            else {
                [item setBadgeValue:nil];
            }
            
            //在桌面图标上显示消息数
            [UIApplication sharedApplication].applicationIconBadgeNumber = unReadCountForNotice;
            
        }
        index++;
    }
}

- (void) loadUnreadSuggestionCount {
    [HttpClient getWithUrl:HTTP_GET_UNREAD_SUGGESTIONS token:[[AccountManager manager]token] paramDic:nil resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
        unReadCountForSuggestion = obj.getUnReadSuggestionResult;
        [self setSuggestionBarView];
    } failure:^(NSError *error) {
        
    }];
}

- (void) setSuggestionBarView {
    
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[self tabBar] items]) {
        //设置BadgeView
        if (index == 3) {
            [item setBadgeBackgroundColor:[UIColor redColor]];
            [item setBadgeTextColor:[UIColor whiteColor]];
            if (unReadCountForSuggestion > 0) {
                [item setBadgeValue:[NSString stringWithFormat:@"%d", unReadCountForSuggestion]];
            }
            else {
                [item setBadgeValue:nil];
            }
            
        }
        index++;
    }
}

@end
